package loadData

import (
	"fmt"
	"github.com/gosuri/uitable"
	"db2s/global"
	"db2s/outPut"
	"strings"
	"sync"
	"sync/atomic"
)

func TLoad(table *uitable.Table, execTime string, measuredPods map[string]*global.TPod, breakPointSum int) {
	//var abnormalCount, normalCount, missCount, podSum int
	//table.AddRow(outputTail(global.TLoad{})...)
	//var seqSort []*global.TPod
	//for _, v := range measuredPods {
	//	podSum++
	//	seqSort = append(seqSort, v)
	//}
	//sort.Slice(seqSort, func(i, j int) bool {
	//	return seqSort[i].Object.Seq < seqSort[j].Object.Seq
	//})
	//for _, pod := range seqSort {
	//	if pod.TableMiss {
	//		missCount++
	//	} else {
	//		switch *pod.Load.SyncStatus {
	//		case -2, -1, 1, 2:
	//			abnormalCount++
	//		case 0:
	//			normalCount++
	//		}
	//	}
	//	table.AddRow(outputValue(pod.Load)...)
	//}
	//resultPrint(table, podSum, breakPointSum, execTime, normalCount, abnormalCount, missCount)
}

var newTer *outPut.Terminal
var outputPrintFunc = global.EFunc{
	FuncName: func(f any) (r any) {
		if f == nil {
			return
		}
		pod := f.(*outPut.TPod)
		switch {
		case pod.TableMiss:
			pod.Result = "miss"
			return "miss"
		case atomic.LoadInt64(pod.Status) == -1 || atomic.LoadInt64(pod.Status) == -2:
			pod.Result = "abnormal"
			return "abnormal"
		default:
			return "ok"
		}
	},
}
var outputExecFunc = global.EFunc{
	FuncName: func(s any) (r any) {
		var writeStr string
		if s == nil {
			return
		}
		switch s.(*outPut.TPod).CheckMode {
		case "struct":
			writeStr = strings.Join(global.StructJsonPointerValueToSlice(s.(*outPut.TPod).Struct), " || ")
		case "object":
			writeStr = strings.Join(global.StructJsonPointerValueToSlice(s.(*outPut.TPod).Object), " || ")
		}
		if err := writeBreakPointData(GlobalPConfigs.FilePoint.ObjectResult, []*string{&writeStr}, 1); err != nil {
			fmt.Println("----error:", err)
			return
		}
		return
	},
}

func terminal(wg *sync.WaitGroup, context *SchedulePlanContext) {
	var initContext = global.GetIoc().GetBean("schemaInitContext").(*SchemaInitContext)
	newTer = outPut.NewTerminal(outPut.Terminal{
		Sum:           initContext.TableSum,
		Wg:            wg,
		Mode:          GlobalPConfigs.rules.CheckMode,
		BreakPointSum: int(atomic.LoadInt64(context.TaskBarTable.BreakPointSum)),
		Pods:          make(chan *outPut.TPod, initContext.TableSum),
		PrintFunc:     outputPrintFunc,
		ExecFunc:      []global.EFunc{outputExecFunc},
	})
	newTer.Start()
	newTer.Run()
}
